<?php

require_once './include/common.inc.php';
require_once JANSEN_ROOT . 'include/cart.class.php';

$page_title = 'jans2en furniture - Login - Login';
$menu_id = 14;
$postiondiv='<div class="position"><a href="index.php">首页</a> &gt; <span class="position"> 登录</span></div>';
$baner_img='<div class="bigtitle">登录</div>';

//$alert_message = "Please enter your username and password to login. ";
$alert_message = "";
$suspended = false;

if ($_POST['remember_account'] == 'on') {
    setCookie('jansenloginname',base64_encode($_POST['username']),time()+3600*24*30);
    setCookie('jansenloginpassword',base64_encode($_POST['password']),time()+3600*24*30);
    
    $loginname = $_POST['username'];
    $loginpassword = $_POST['password'];
    $checked   = "checked"; 
    
} elseif (isset($_POST['username']) && $_POST['remember_account'] != 'on') {
//} else {
    setCookie('jansenloginname','',0);
    setCookie('jansenloginpassword','',0);
    //unset($_COOKIE['jansenloginname']);
    //unset($_COOKIE['jansenloginpassword']);
    
    $loginname = '';
    $loginpassword = '';
    $checked   = ""; 
    
} else {
    $loginname = isset($_COOKIE['jansenloginname']) && !empty($_COOKIE['jansenloginname']) ? base64_decode($_COOKIE['jansenloginname']) : '';
    $loginpassword = isset($_COOKIE['jansenloginpassword']) && !empty($_COOKIE['jansenloginpassword']) ? base64_decode($_COOKIE['jansenloginpassword']) : '';
    $checked = isset($_COOKIE['jansenloginname']) && !empty($_COOKIE['jansenloginname']) ? " checked " : ' ';
}

if ((isset($_GET['action']) && $_GET['action'] == 'logout') || (isset($_POST['action']) && $_POST['action'] == 'logout')) {
	browseTracking(8,0,'Logout');
    Cookie('jansenuser','',0);
    header("Location: index.php");
    exit;
} elseif(isset($_POST['username']) && isset($_POST['password']) && !empty($_POST['username']) && !empty($_POST['password'])){
    $rs = $db->get_one("SELECT * FROM user WHERE username='" . $_POST['username'] . "' AND `type` < 5");
    if(md5($_POST['password']) == $rs['password']){
        // ============ Temporary Customer start ======================

        if ($rs['status'] == 1) {
            if ($rs['firstlogin'] == 0) {
                $sql = "UPDATE `user` SET `firstlogin` = '$timestamp' WHERE username='" . $_POST['username'] . "'";
                $db->query($sql);
            } elseif ($timestamp-$rs['firstlogin'] > 3600*24*3 && $timestamp-$rs['admin_settime'] > 3600*24*3) {
                $sql = "UPDATE `user` SET `status` = '3' WHERE username='" . $_POST['username'] . "'";
                $db->query($sql);
                $alert_message = "<font color=red>Your account has suspended .</font>";
                $suspended = true;
            } else {
                // 樟哿彸蚚
            }
        } elseif ($rs['status'] == 2) {

        } else {
            $alert_message = "<font color=red>Your account has suspended .</font>";
            $suspended = true;
        }
        // ============ Temporary Customer end ========================
        if (!$suspended) {
            // 載陔 browse_tracking
            browseTracking_login($rs['id']);
            
            Cookie("jansenuser",base64_encode($rs['id']."\t".$rs['type']."\t".$rs['status']."\t".$timestamp), $cookietime);            
            $sql = "UPDATE `user` SET `lastlogin` = `currlogin`, `currlogin` = '$timestamp' WHERE username='" . $_POST['username'] . "'";
            $db->query($sql);
			
			/*
			$sql = "select cart from cart_record where uid = " . $rs['id'];			
			$cart_t = $db->get_one($sql);
			if (!empty($cart_t)) {
				$cart = new cart();
				$cart_g = unserialize($cart_t['cart']);
				if (empty($cart_g->myCart)) {
					@saveCart($db, $rs['id'], "");
				} else {
					$_SESSION["myCart"] = $cart_g->myCart;
					$cart->update();
				}			
			}*/
			
			//$redirectUrl = "index.php";
			$redirectUrl='product_list.php?categoryid=1&fid=0';
			if($_POST["preUrl"]!="")
				$redirectUrl = base64_decode($_POST["preUrl"]);

			header("Location: ".$redirectUrl);
            exit;
        }



    } else {
        $alert_message = "<font color=red>用户名称或密码不正确！</font> <br><font color=red>请重试！</font>";
    }
} elseif (isset($_POST['action']) && $_POST['action'] == 'login') {
    $alert_message = "<font color=red>请输入用户名称和密码。 </font>";
}

if($_GET["url"]!="")
	$preUrl =  $_GET["url"];

// -------------- guideͼƬ start ----------------------------------------
$img = getSystemValue("picture_other_loginen");

if (trim($img) == "" || !file_exists("attachments/system/" . $img)) {
	$img = 'attachments/noCN.gif';
} 
else
{
	$img = 'attachments/system/'.$img;	
}
// -------------- guideͼƬ end ----------------------------------------

require_once PrintEot('header');
require_once PrintEot('login');
require_once PrintEot('footer');
?>